//请指定操作的表格Id,以免多次指定
dataTableJs.tableId = 'role-table';
var oTable1;
var roleTableChanges = {};
$(function () {
    oTable1 =
        $('#' + dataTableJs.tableId).dataTable($.extend(dataTableJs.defaults, {
            "dom": 't<"row"<"col-xs-12"pri>>',
            "ajax": {
                url: ctx + "/admin/authorization/list?adminId=" + $("#adminId").val(), data: function (d) {
                    delete d.columns;
                    //增加page和pageSize
                    if (oTable1) {
                        d._page = oTable1.api().page();
                        d._pageSize = oTable1.api().page.len();
                    }
                    $("#roleTableChanges").val(JSON.stringify(roleTableChanges));
                    //增加查询
                    return dataTableJs.serialize(d, "#search-form");
                }
            },
            "iDisplayLength":5,
            "drawCallback": function (settings) {
                $('#' + dataTableJs.tableId + '> tbody > tr >td input[type=checkbox]').each(function () {
                    var $this = $(this);
                    var ck = roleTableChanges[$this.val()];
                    if (ck != null) {
                        $this.attr("checked", ck);
                    }
                })
            },
            "order": [[1, "desc"]], /**默认排序*/
            "columns": [
                {"data": null, orderable: false},
                {"data": "name"},
                {"data": "description", orderable: false},
                {"data": "createDate"}


            ],
             columnDefs: [
                {
                    targets: 0,
                    render: function (a, b, c, d) {
                        return '<div class="center"><label class="pos-rel"><input type="checkbox" class="ace"  ' + (c.isCheck ? 'checked=checked' : '') + ' value="' + c.id + '"> <span class="lbl"></span></label></div>';
                    }
                }

            ]
        }));
    rememberCheck();
})


function rememberCheck() {
    //全选
    $("#" + dataTableJs.tableId + ' > thead > tr > th input[type=checkbox]').eq(0).on('click', function () {
        var th_checked = this.checked;//checkbox inside "TH" table header
        $(this).closest('table').find('tbody > tr').each(function () {
            var row = this;
            var checkBox = $(row).find('input[type=checkbox]').eq(0);
            roleTableChanges[checkBox.val()] = th_checked;
        });
    });
    //单选
    $('#' + dataTableJs.tableId).on('click', 'td input[type=checkbox]', function () {
        roleTableChanges[this.value] = this.checked;
    });
}

/**查询*/
function doSearch(){
    oTable1.api().ajax.data={"params": $("#search-form").serialize()};
    oTable1.api().ajax.reload();
}

/**
 * 保存
 */
function doSave() {
    var url = ctx + "/admin/authorization/insert";
    var adminId = $("#adminId").val();
    $.getJSON(url, {ids: JSON.stringify(roleTableChanges), adminId: adminId}, function (data) {
        $.messager.alert0({selector: "#indexAlert", type: data.status, html: data.body});
        if (data && data.status == 'SUCCESS') {
            $.bootbox.hideAll();
        }
    });
}
